package com.rst.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.rst.model.Country;
import com.rst.model.Currency;
import com.rst.model.InstituteDetail;
import com.rst.model.Student;
import com.rst.model.StudentCategory;
import com.rst.model.TimeZone;

@Repository
@Transactional
public class IndexDAO {

	@Autowired
	private SessionFactory sessionFactory;

	@SuppressWarnings("unchecked")
	public List<Student> getAllStudent()
	{
		Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Student.class);
		return criteria.list();
	}

	public Long save(Student student)
	{
		return (Long) sessionFactory.getCurrentSession().save(student);
	}

	@SuppressWarnings("unchecked")
	public List<TimeZone> getTimeZones() {
		Criteria criteria = sessionFactory.getCurrentSession().createCriteria(TimeZone.class);
		return criteria.list();
	}

	@SuppressWarnings("unchecked")
	public List<StudentCategory> getStudentCategory() {
		Criteria criteria = sessionFactory.getCurrentSession().createCriteria(StudentCategory.class);
		return criteria.list();
	}

	@SuppressWarnings("unchecked")
	public List<Currency> getCurrenciesList() {
		Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Currency.class);
		return criteria.list();
	}

	public Long saveInstituteDetail(InstituteDetail detail) {
		      
		sessionFactory.getCurrentSession().save(detail);
		return  detail.getInstituteId();
		
	}

	public InstituteDetail getInstituteDetailById(Long instituteId) {
	    Session session = sessionFactory.getCurrentSession();
	    
/*	    InstituteDetail instituteDetail = (InstituteDetail)session.get(InstituteDetail.class, instituteId);
	    Hibernate.initialize(instituteDetail.getTimeZone());
	    Hibernate.initialize(instituteDetail.getCurrency());
	    Hibernate.initialize(instituteDetail.getCity());
*/	    
	    String str = "FROM InstituteDetail i  LEFT JOIN FETCH i.city ci LEFT JOIN FETCH ci.state s LEFT JOIN FETCH s.country con LEFT JOIN FETCH i.currency cu LEFT JOIN FETCH i.timeZone tz WHERE i.instituteId = :instituteId";

	    Query query = session.createQuery(str);
	    query.setParameter("instituteId",  instituteId);		
		return (InstituteDetail) query.uniqueResult();
	}

	
			
	public Long udateInstituteDetail(InstituteDetail detail) {
		sessionFactory.getCurrentSession().update(detail);
		return  detail.getInstituteId();
	}

	@SuppressWarnings("unchecked")
	public List<Object[]> getCityNameList(String name) {
		Session session = sessionFactory.getCurrentSession();
		// session.setFlushMode(FlushMode.MANUAL);
		StringBuilder queryStr = new StringBuilder("SELECT c.f_city_id, c.f_city_name, s.f_state_name, ct.f_country_name FROM t_city c, t_state s ,t_country ct WHERE ")
		.append(" c.f_city_name LIKE :name AND c.f_state_id = s.f_state_id AND s.f_country_id = ct.f_country_id")
		.append(" ORDER BY c.f_city_name");
		Query query = session.createSQLQuery(queryStr.toString());
		// query.setReadOnly(true);
		query.setParameter("name",  "%" + name + "%");
		return query.list();
	}

	@SuppressWarnings("unchecked")
	public List<Country> getCountryList() {
		Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Country.class);
		return criteria.list();
	}
}
